﻿@model Yes.Web.Blog.Areas.Admin.Models.Article.AddView
@{
    ViewBag.Title = "Add";
    Layout = "~/Areas/Admin/Views/Shared/_Layout.cshtml";
}


@section CssJs{
<script type="text/javascript" charset="utf-8" src="/ueditor/ueditor.config.js"></script>
<script type="text/javascript" charset="utf-8" src="/ueditor/ueditor.all.js"> </script>
<script type="text/javascript" charset="utf-8" src="/ueditor/lang/zh-cn/zh-cn.js"></script>

}




<div class="main-wrap">
    <div class="crumb-wrap">
        <div class="crumb-list"><i class="icon-font"></i><a href="@(Url.Action("Index","Home"))">首页</a><span class="crumb-step">&gt;</span><a class="crumb-name" href="@(Url.Action("Index","Article"))">文章管理</a><span class="crumb-step">&gt;</span><span>新增文章</span></div>
    </div>
    <div class="result-wrap">
        <div class="result-content">
            <form action="@(Url.Action("Add", "Article"))" method="post">
                <input  type="hidden" name="Id" value="@(Model.Article.Id)">
                <table class="insert-table" width="100%">
                    <tbody>
                    <tr>
                        <th width="120"><i class="require-red">*</i>分类：</th>
                        <td>
                            <select name="CategoryId" id="CategoryId" class="required">
                                <option value="">请选择</option>
                                @foreach (var cate in Model.CategoryList)
                                {
                                    if (cate.Id == Model.Article.CategoryId)
                                    {
                                        <option value="@(cate.Id)" selected="selected">@(cate.Name)</option>
                                    }
                                    else
                                    {
                                        <option value="@(cate.Id)">@(cate.Name)</option>
                                    }

                                }
                            </select>
                        </td>
                    </tr>
                    <tr>
                        <th><i class="require-red">*</i>标题：</th>
                        <td>
                            <input class="common-text required" id="title" name="title" size="50" value="@(Model.Article.Title)" type="text">
                        </td>
                    </tr>
                        <tr>
                            <th>标签：</th>
                            <td>
                                <input class="common-text" id="Tag" name="Tag" size="50" value="@(Model.Article.Tag)" type="text">
                            </td>
                        </tr>
                    <tr>
                        <th><i class="require-red"></i>封面图：</th>
                        <td>
                            <script type="text/plain" id="ArticleImg"></script>
                            <input class="common-text" id="Img" name="Img" size="50" value="@(Model.Article.Img)" type="text">
                            <input class="btn" id="selectImg" value="选择图片" type="button">
                        </td>
                    </tr>
                    <tr>
                        <th><i class="require-red"></i>属性：</th>
                        <td>
                            可否评论： <input id="CanComment" name="CanComment"  @if (Model.Article.CanComment) { Output.Write("checked"); } value="true" type="checkbox">

                        </td>
                    </tr>
                    <tr>
                        <th>摘要：</th>
                        <td>
                            <textarea name="Summary" id="Summary" rows="5" cols="80">@Html.Raw(Model.Article.Summary)</textarea>
                        </td>
                    </tr>
                    <tr>
                        <th>内容：</th>
                        <td>
                            <script id="content" name="content" type="text/plain" style="width: 100%; height: 500px;">
                                @Html.Raw(Model.Article.Content)   
                            </script>

                        </td>
                    </tr>
                    <tr>
                        <th></th>
                        <td>
                            <input class="btn btn-primary btn6 mr10" value="保存" type="button" id="addBtn">

                            @if (Model.Article.Id == 0)
                            {
                                <input class="btn btn-primary btn6 mr10" value="保存草稿" type="button" id="saveBtn">
                            }


                            <input class="btn btn6" onclick="history.go(-1)" value="返回" type="button">
                        </td>
                    </tr>
                    </tbody>
                </table>
            </form>
        </div>
    </div>

</div>
<script>
    var _editor;
    $(function () {
        //var editor = CKEDITOR.replace('content');

        var editor = UE.getEditor('content',
            {
                toolbar: [
                    ['fullscreen', 'source', 'undo', 'redo', 'bold', 'italic',
                        'underline', 'fontborder', 'backcolor', 'fontsize', 'fontfamily',
                        'justifyleft', 'justifyright', 'justifycenter', 'justifyjustify',
                        'strikethrough', 'superscript', 'subscript', 'removeformat',
                        'formatmatch', 'autotypeset', 'blockquote', 'pasteplain', '|',
                        'forecolor', 'backcolor', 'insertorderedlist', 'insertunorderedlist',
                        'selectall', 'cleardoc', 'link', 'unlink', 'emotion']
                ]
            });

        _editor = UE.getEditor('ArticleImg');
        _editor.ready(function () {
            _editor.setDisabled();
            _editor.hide();//隐藏UE框体  
            _editor.addListener('beforeinsertimage', function (t, arg) {
                $("#Img").val(arg[0].src);
            });
        });  


        $(document).on('click', '#addBtn', function () {
            $("#content").val(UE.getEditor('content').getContent());
            $.ajax({
                type: "POST",
                url: "@(Url.Action("Add","Article"))",
                data: $("form").serialize(),
                success: function (result) {
                    if (result.status) {
                        history.go(-1);
                    } else {
                        var error = result.errorMessage;
                        if (result.data) {
                            error += "<br>" + result.data;
                        }
                        layer.alert(error, { icon: 5 });
                    }
                }
            });
        }).on('click', '#saveBtn', function () {
            $("#content").val(editor.getData());
            $.ajax({
                type: "POST",
                url: "@(Url.Action("Save","Article"))",
                data: $("form").serialize(),
                success: function (result) {
                    if (result.status) {
                        location.href = "@(Url.Action("Draft", "Article"))";
                    } else {
                        var error = result.errorMessage;
                        if (result.data) {
                            error += "<br>" + result.data;
                        }
                        layer.alert(error, { icon: 5 });
                    }
                }
            });
        }).on('click', '#selectImg', function () {
            /*
            layer.open({
                type: 2,
                title: '选择图片',
                shadeClose: true,
                shade: false,
                area:["800px","550px"],
                content: '/admin/file/browser?type=image&CKEditor=Img&CKEditorFuncNum=1&langCode=zh-cn'
            });*/

            var d = _editor.getDialog("insertimage");
            d.render();  
            d.open();
            });
    });


</script>